/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.dialog.NetworkDialog;
import com.shwread.http.base.TextHttpResponseHandler;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.apache.http.Header;

import base.tina.core.task.infc.ITaskResult;
import logic.util.NetworkUtil;
import logic.util.Util;


public class PersonalChangeNamePage
        extends
        AbstractUIPage<MainActivity>
        implements
        OnClickListener {
    private TextView tvTitle;
    private Button btnBack;
    private EditText etNickName;
    private Button btnSubmit;

    private UserBean user;
    private InputMethodManager inputMethodManager;

    //	private static PersonalChangeNamePage instance;
    //
    //	public static PersonalChangeNamePage getInstance(PersonalInfoActivity t) {
    //		if (null == instance) instance = new PersonalChangeNamePage(t);
    //		return instance;
    //	}

    /**
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param t
     */
    public PersonalChangeNamePage(MainActivity t, UserBean user) {
        super(t);
        this.user = user;
    }

    @Override
    public <E extends Activity> View createView(E e, int initializers) {
        if (null != curMyView) return curMyView;
        curMyView = bActivity.getLayoutInflater().inflate(R.layout.user_info_setting_change_nickname, null);
        inputMethodManager = (InputMethodManager) bActivity.getSystemService(Context.INPUT_METHOD_SERVICE);

        initView();
        setListener();

        return curMyView;
    }

    void initView() {
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText("修改昵称");
        btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
        etNickName = (EditText) curMyView.findViewById(R.id.user_info_setting_change_nickname_et);
        btnSubmit = (Button) curMyView.findViewById(R.id.user_info_setting_change_nickname_submit_tv);
        etNickName.setText(Util.isEmpty(user.getNickName()) ? "" : user.getNickName());
    }

    void setListener() {
        btnBack.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public View updateView(int toStatus, ITaskResult data) {
        return null;
    }

    @Override
    public void notifyView(int cmd, Bundle bundle) {

    }

    @Override
    public int enter(IUIPage<?> prePage) {
        etNickName.requestFocus();
        inputMethodManager.showSoftInput(etNickName, InputMethodManager.RESULT_UNCHANGED_SHOWN);
        return 0;
    }

    @Override
    public int leave(IUIPage<?> nextPage) {
        Util.hideInputMethodManager(context, etNickName);
        return 0;
    }

    @Override
    public void setStatus(int status) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                bActivity.onBackPressed();
                break;
            case R.id.user_info_setting_change_nickname_submit_tv:
                clickSubmit();
                break;
            default:
                break;
        }

    }

    private String nickName;

    void clickSubmit() {
        nickName = etNickName.getText().toString().trim();
//		user.setNickName(etNickName.getText().toString().trim());
        if (NetworkUtil.isNetAvailable(bActivity)) {
            if (Util.isNotEmpty(nickName)) {
                if (Util.isIncludeSpecificSymbol(nickName)) {
                    Util.showToast(bActivity, "昵称不能加入特殊符号");
                } else {
//					new ModAccountInfoAction(bActivity, nickName, "", "", "", "", "", new InfoListener()).start();
                    NewActionHelp.modAccountInfo(context, nickName, "", "", "", "", "", new InfoHandler());

                }
            } else {
                Util.showToast(bActivity, "请输入昵称");
            }
        } else {
            new NetworkDialog(bActivity).show();
        }
    }

    class InfoHandler extends TextHttpResponseHandler {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            showToast("保存失败，请稍候再试", false);

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            user.setNickName(nickName);
            Util.showToast(bActivity, "保存成功");
            bActivity.onBackPressed();
            //					PersonalInfoPage.getInstance(bActivity).setNickName(mStrNickName);
            //					bActivity.changePage(PersonalInfoPage.getInstance(bActivity), KEEP_LAST_STATUS);

        }

    }


    class InfoListener
            implements
            ActionListener {

        @Override
        public void OK(Object obj) {
            bActivity.getUIHandler().post(new Runnable() {

                @Override
                public void run() {
                    user.setNickName(nickName);
                    Util.showToast(bActivity, "保存成功");
                    bActivity.onBackPressed();
                    //					PersonalInfoPage.getInstance(bActivity).setNickName(mStrNickName);
                    //					bActivity.changePage(PersonalInfoPage.getInstance(bActivity), KEEP_LAST_STATUS);
                }
            });
        }

        @Override
        public void ERROR(int resultCode, String message) {
            showToast("保存失败，请稍候再试", false);

        }

        @Override
        public void HTTPERROR(int statusCode) {
            // TODO Auto-generated method stub

        }

    }

    void showToast(final String message, final boolean isClosed) {
        bActivity.getUIHandler().post(new Runnable() {

            @Override
            public void run() {
                Util.showToast(bActivity, message);
                if (isClosed) bActivity.onBackPressed();
            }
        });
    }
}
